﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.fountain {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import reprise.controls.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import __AS3__.vec.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.fountain.*;

    public class FountainGemsPopupMediator extends AbstractFountainPopupMediator {

        public var symbols:SymbolModel;
        public var tutorial:TutorialModel;
        public var paymentModel:PaymentModel;
        private var _hasAllSymbols:Boolean;// = false
        private var _neededSymbols:Object;
        private var _askButtons:Dictionary;

        private static const SYMBOLS:Array = [GameConstants.LOVE, GameConstants.FIRE, GameConstants.AIR, GameConstants.WATER, GameConstants.STAR, GameConstants.ICE];

        public function FountainGemsPopupMediator(){
            this._askButtons = new Dictionary();
            super();
        }
        override public function onRegister():void{
            super.onRegister();
            addContextListener(EventConstants.CLOSE_POPUP_BY_TYPE, this.onClosePopup, InfoEvent);
            _popupType = ProtocolConstants.BUY_GEMS;
            this._neededSymbols = this.view.data.symbols;
            this._hasAllSymbols = this.symbols.hasSymbols(this._neededSymbols);
            this.setPopupState();
            this.createSymbolOverview();
            addPodest(_gemContainer);
        }
        override protected function onClick(_arg1:MouseEvent):void{
            var _local2:String;
            var _local3:InfoEvent;
            if (this._hasAllSymbols){
                _local2 = this.view.data.patientId;
                _local3 = new InfoEvent(EventConstants.START_REGENERATION, _local2);
                dispatch(_local3);
            };
            super.onClick(_arg1);
        }
        private function setPopupState():void{
            var _local1:String;
            if (this._hasAllSymbols){
                addFountainSubHeader("fountain_collect_allcollected", null, _content);
                addActionButton("fountain_button_start_now", _content);
            } else {
                _local1 = (this.isSpecialTutorialStep) ? "fountain_collect_description" : "fountain_collect_notcollected";
                addFountainSubHeader(_local1, null, _content);
                if (this.isSpecialTutorialStep){
                    addActionButton("fountain_button_collect_now", _content);
                } else {
                    addActionButton("symbols_buy_start", _content, this.onBuyStart);
                    this.addGemHint(_content);
                };
            };
        }
        private function addGemHint(_arg1:UIComponent):void{
            addLabel("gemHint myriadProFontNormal", "fountain_hint_description", null, _arg1);
        }
        private function createSymbolOverview():void{
            var _local1:int;
            var _local3:int;
            var _local4:UIComponent;
            var _local5:Boolean;
            var _local8:String;
            var _local9:int;
            var _local2:int;
            var _local6:Vector.<String> = this.getNeededKeys();
            if ((_local6.length <= 1)){
                _gemContainer.addCSSClass("singleGemsContainer");
                //unresolved jump
            };
            var _local7:int = ((_local6.length > 1)) ? GameConstants.FOUNTAIN_MULTIPLE_GEM_WIDTH : GameConstants.FOUNTAIN_SINGLE_GEM_WIDTH;
            for each (_local8 in _local6) {
                _local1 = int(this._neededSymbols[_local8]);
                _local2++;
                _local3 = Numbers.constrain(int(this.symbols.symbols[_local8]), 0, _local1);
                _local5 = (_local3 >= _local1);
                _local4 = RepriseUtils.addComponent(_gemContainer, "fountainRewardContainer");
                this.addGemIcon(_local4, _local8, _local7);
                if (_local5){
                    this.addSolvedIcon(_local4);
                } else {
                    addLabel("neededLabel myriadProFontOutline", "global_of", {has:_local3, needed:_local1}, _local4);
                    if (!this.isSpecialTutorialStep){
                        this.addAskButton(_local4, _local8);
                    };
                };
                if ((((_local2 >= 1)) && ((_local2 < _local6.length)))){
                    this.addEqualsPlusIcon(_gemContainer, "plusIcon");
                };
            };
            this.addEqualsPlusIcon(_gemContainer, "equalsIcon");
            _local9 = (((_local6.length + 1) * GameConstants.FOUNTAIN_GEM_CONTAINER_WIDTH) + GameConstants.FOUNTAIN_REWARD_CONTAINER_SPACER);
            _gemContainer.setStyle("width", _local9.toString());
        }
        private function getNeededKeys():Vector.<String>{
            var _local2:String;
            var _local1:Vector.<String> = new Vector.<String>();
            for each (_local2 in SYMBOLS) {
                if (int(this._neededSymbols[_local2])){
                    _local1.push(_local2);
                };
            };
            return (_local1);
        }
        private function addGemIcon(_arg1:UIComponent, _arg2:String, _arg3:int):void{
            var target = _arg1;
            var key = _arg2;
            var iconDim = _arg3;
            var iconClip:MovieClip = AssetsLibrary.getMovieClip((("Reward" + Strings.capitalize(key)) + "Thumb"));
            var icon:UIComponent = RepriseUtils.addClipComponent(target, "fountainIcon", iconClip);
            var ratio:Number = (iconClip.width / iconClip.height);
            var iconWidth:Number = (iconDim * ratio);
            icon.setStyle("width", iconWidth.toString());
            target.setStyle("width", iconWidth.toString());
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, 0);
            });
        }
        private function addAskButton(_arg1:UIComponent, _arg2:String):void{
            var _local3:LabelButton;
            if (!sessionModel.getRequestLimit(_arg2)){
                _local3 = createButton(_arg1, "askButton blueButton", "global_button_ask", "myriadProFont");
                this._askButtons[_arg2] = _local3;
                _local3.setData({gemId:_arg2});
                eventMap.mapListener(_local3, MouseEvent.CLICK, this.onAskClick);
            };
        }
        private function addSolvedIcon(_arg1:UIComponent):void{
            var target = _arg1;
            var solvedClip:MovieClip = AssetsLibrary.getMovieClip("SolvedIcon");
            var solvedIcon:UIComponent = RepriseUtils.addClipComponent(target, "solvedIcon", solvedClip);
            RepriseUtils.validateComponent(solvedIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function addEqualsPlusIcon(_arg1:UIComponent, _arg2:String):void{
            var target = _arg1;
            var clipName = _arg2;
            var plusContainer:UIComponent = RepriseUtils.addComponent(target, "fountainPlusContainer");
            var plusClip:MovieClip = AssetsLibrary.getMovieClip(clipName);
            var plusIcon:UIComponent = RepriseUtils.addClipComponent(plusContainer, "fountainPlusIcon", plusClip);
            RepriseUtils.validateComponent(plusIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function onClosePopup(_arg1:InfoEvent):void{
            if (_arg1.id == _popupType){
                close();
            };
        }
        private function onBuyStart(_arg1:MouseEvent):void{
            var _local2:Array = (this.paymentModel.getData(ProtocolConstants.BUY_GEMS) as Array);
            var _local3:Object = Arrays.findByAttribute(_local2, "name", ProtocolConstants.INSTANT_FOUNTAIN_START);
            var _local4:Object = {product_type:ProtocolConstants.BUY_GEMS, product_id:_local3.product_id, icon:_local3.icon};
            var _local5:InfoEvent = new InfoEvent(EventConstants.START_PAYMENT, null, _local4);
            dispatch(_local5);
        }
        private function onAskClick(_arg1:MouseEvent):void{
            var _local2:LabelButton = (_arg1.target as LabelButton);
            var _local3:String = _local2.data().gemId;
            var _local4:InfoEvent = new InfoEvent(GameConstants.ASK_FOR_SYMBOL, _local3);
            dispatch(_local4);
        }
        override protected function hideAskButton(_arg1:String):void{
            var _local2:LabelButton = (this._askButtons[_arg1] as LabelButton);
            ((_local2) && (_local2.setStyle("visibility", "hidden")));
        }
        private function get isSpecialTutorialStep():Boolean{
            return ((this.tutorial.state <= TutorialModel.FOUNTAIN_SHOW_ELDER_BUBBLE));
        }
        private function get view():FountainGemsPopupView{
            return ((viewComponent as FountainGemsPopupView));
        }

    }
}//package net.wooga.pets.views.popups.fountain 
